package com.tenglong.teacher.ui.register;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.netease.nim.uikit.common.ToastHelper;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;



/**
 * Created by admin on 2019/5/30.
 */

public class ChoiceSchoolListActivity extends BaseActivity implements View.OnClickListener {

    //View
    private ListView listView;
    //Model：定义的数据
    private List<ClientUserInfo> list;
    private String name;
    private RequestOptions options;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choiceschool);
        setTitle("选择机构");
        options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.xuexiao).
                placeholder(R.drawable.xuexiao);

        listView = (ListView) findViewById(R.id.listView);
        showProgressDialog("加载中");
        getData();

    }

    private void getData() {
        CreateRequestEntity.getWebServiceLogin().getAllCompany().enqueue(new Callback<BaseResult<List<ClientUserInfo>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<ClientUserInfo>>> call, Response<BaseResult<List<ClientUserInfo>>> response) {
                dismissProgressDialog();
                if(ResultVerifier.isSucceed(response)&&response.body().data!=null){
                    list=response.body().data;
                    listView.setAdapter(new MyAdapter());
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                            setResult(RESULT_OK,getIntent().putExtra("companyInfo",list.get(position)));
                            finish();
                        }
                    });
                }else{
                    ToastHelper.showToast(ChoiceSchoolListActivity.this,response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseResult<List<ClientUserInfo>>> call, Throwable t) {
                dismissProgressDialog();
                ToastHelper.showToast(ChoiceSchoolListActivity.this,t.getMessage());
            }
        });
    }




    //为ExpandableListView自定义适配器
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder mHolder;
            if (convertView == null) {
                mHolder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.choice_school_item, null, true);
                mHolder.img = convertView.findViewById(R.id.img);
                mHolder.headTxt = convertView.findViewById(R.id.headTxt);
                mHolder.title = convertView.findViewById(R.id.title);
                mHolder.type = convertView.findViewById(R.id.type);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }
            mHolder.title.setText(list.get(position).getCompanyName());
            if(position==0){
                mHolder.headTxt.setVisibility(View.VISIBLE);
            }  else{
                mHolder.headTxt.setVisibility(View.GONE);
            }


            Glide.with(ChoiceSchoolListActivity.this)
                    .load(IpUtils.Ips[0]+list.get(position).getCompanyPhotoUrl())
                    .apply(options)
                    .into(mHolder.img);
            return convertView;
        }

    }
    // ViewHolder用于缓存控件，属性分别对应item布局文件的控件
    class ViewHolder{
         ImageView img;
         TextView headTxt;
         TextView title;
         TextView type;
    }

}

